package com.qx.dao;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.qx.entity.Roles;
import com.qx.entity.Users;


public interface UsersDao extends JpaRepository<Users, Integer>,JpaSpecificationExecutor<Users>{
//盐值加密验证登录
	@Query(value="select *from users where loginname=?1  ",nativeQuery=true)
	public Users getUsers(String name);
//是否锁定账户
	@Transactional
	@Modifying
	@Query(value="update  users set islockout=?1 where id=?2",nativeQuery=true)
	public Integer updZhangHu(String z,Integer id);
//查询用户拥有的角色
	@Query(value="select userid,roleid from users u,roles r,userroles ur WHERE u.id=ur.userid AND r.id=ur.userid AND u.id=?1",nativeQuery=true)
	//public List<Users> getById(Integer userid);
	public List<Map<String, Object>> getById(Integer userid);//高端操作  社会你曹哥
//用户角色的添加
	@Transactional
	@Modifying
	@Query(value="insert INTO userroles VALUES(?1,?2)",nativeQuery=true)
	public Integer addUs(Integer userid,Integer roleid);
//用户角色的删除
	@Transactional
	@Modifying
	@Query(value="DELETE FROM userroles WHERE userid=?1 AND roleid=?2",nativeQuery=true)
	public Integer delUs(Integer userid,Integer roleid);	
//用户密码初始化
	@Transactional
	@Modifying
	@Query(value="UPDATE users SET password = :#{#u.password} WHERE id=:#{#u.id} ",nativeQuery=true)  
	public Integer updUsp(Users u);
//用户登录时，记录登陆时间
	@Transactional
	@Modifying
	@Query(value="UPDATE users SET lastlogintime= :#{#u.lastlogintime} WHERE id=:#{#u.id} ",nativeQuery=true)  
	public Integer updUsTime(Users u);
//用户登录时，如果登录5次没有成功，将锁定用户
	@Transactional
	@Modifying
	@Query(value="UPDATE users SET psdwrongtime=psdwrongtime+1  WHERE id=?1 ",nativeQuery=true)  
	public Integer updUsSuo(Integer id);
//每次更改用户账户的状态之后，都要把错误次数归零	
	@Transactional
	@Modifying
	@Query(value="UPDATE users SET psdwrongtime=0  WHERE id=?1 ",nativeQuery=true)  
	public Integer updUsSuoTimes(Integer id);	
}
